package com.clayecloud.module_house.viewmodels;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_common.apis.HouseApi;
import com.clayecloud.lib_common.apis.entity.house.ClientHouse;
import com.clayecloud.lib_common.apis.entity.house.ClientUserBindHouse;
import com.clayecloud.lib_common.room.entity.ClientUser;
import com.clayecloud.lib_common.utils.GsonUtil;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.lib_common.utils.MessageUtil;
import com.clayecloud.module_house.entity.House;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ActivityHouseMainViewModel extends ViewModel {

    // houseAPI
    private HouseApi houseApi;

    // 房屋数据
    private MutableLiveData<List<House>> houseList;

    public MutableLiveData<List<House>> getHouseList() {
        return houseList;
    }

    public void setHouseList(MutableLiveData<List<House>> houseList) {
        this.houseList = houseList;
    }

    /**
     * 初始化数据
     */
    public void leadData(){
        // 初始化房屋API
        houseApi = HttpUtil.getInstance().create(HouseApi.class);
        // 获取房屋信息
        List<House> houses = new ArrayList<>();
        MutableLiveData<List<House>> houseList = new MutableLiveData<>();
        setHouseList(houseList);
        houseApi.getUserBindAllHouse(LoginUtil.getUserID()).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {

                        List<ClientHouse> clientHouses = GsonUtil.getInstance().fromJson(GsonUtil.getInstance().toJson(messageUtil.getObj()),new TypeToken<List<ClientHouse>>() {}.getType());
                        for(ClientHouse clientHouse:clientHouses){
                            houses.add(new House(clientHouse.getHouseId(),clientHouse.getHouseAddrass(),clientHouse.getHouseResidentialQuarters(),clientHouse.getHouseNumber()
                            ,"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.518fang.com%2Fimg3%2F518upload%2Fxqt%2F201409%2F541%2F389%2Fcbaf9dd95abc450e846b009517ebfeee.jpg&refer=http%3A%2F%2Fimg.518fang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642429972&t=6b42ed039f9604531225e927e6e30eec",
                                  "商品房·精装修三居"));
                            System.out.println(clientHouse.toString());
                        }
                        houseList.setValue(houses);
                        setHouseList(houseList);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("ActivityHouseMainViewModel => leadData => onError",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }

    /**
     * 添加回调的函数
     */
    public void addHouse(ClientUserBindHouse clientUserBindHouse){
        MutableLiveData<List<House>> houseList = getHouseList();
        List<House> list = houseList.getValue();
        list.add(new House(clientUserBindHouse.getHouseId(),clientUserBindHouse.getHouseAddrass(),clientUserBindHouse.getHouseResidentialQuarters(),clientUserBindHouse.getHouseNumber()
                ,"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.518fang.com%2Fimg3%2F518upload%2Fxqt%2F201409%2F541%2F389%2Fcbaf9dd95abc450e846b009517ebfeee.jpg&refer=http%3A%2F%2Fimg.518fang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642429972&t=6b42ed039f9604531225e927e6e30eec",
                "商品房·精装修三居"));
        houseList.setValue(list);
        setHouseList(houseList);
    }

    /**
     * 刷新整个页面
     */
    public void updateHouse(){
        houseApi.getUserBindAllHouse(LoginUtil.getUserID()).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        MutableLiveData<List<House>> houseList = getHouseList();
                        List<House> list = houseList.getValue();
                        list.clear();
                        List<ClientHouse> clientHouses = GsonUtil.getInstance().fromJson(GsonUtil.getInstance().toJson(messageUtil.getObj()),new TypeToken<List<ClientHouse>>() {}.getType());
                        for(ClientHouse clientHouse:clientHouses){
                            list.add(new House(clientHouse.getHouseId(),clientHouse.getHouseAddrass(),clientHouse.getHouseResidentialQuarters(),clientHouse.getHouseNumber()
                                    ,"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.518fang.com%2Fimg3%2F518upload%2Fxqt%2F201409%2F541%2F389%2Fcbaf9dd95abc450e846b009517ebfeee.jpg&refer=http%3A%2F%2Fimg.518fang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642429972&t=6b42ed039f9604531225e927e6e30eec",
                                    "商品房·精装修三居"));
                        }
                        houseList.setValue(list);
                        setHouseList(houseList);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("ActivityHouseMainViewModel => leadData => onError",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }

    /**
     * 删除
     */
    public void deletehouse(int position){
        houseApi.deleteHouse(houseList.getValue().get(position).getHouseID(),LoginUtil.getUserID()).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if(messageUtil.getResult()){
                            MutableLiveData<List<House>> houseList = getHouseList();
                            List<House> list = houseList.getValue();
                            list.remove(position);
                            houseList.setValue(list);
                            setHouseList(houseList);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("ActivityHouseMainViewModel => deletehouse => onError",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }

}
